package com.mfnote.util;

public class StringUtils {

	
	/*
	 * @param string
	 * @return 光标的位置
	 */
	public static int getIndex(String string) {
		if("".equals(string)){
			return 0;
		}
		String strBlank = "\t";
		String strNextLine = "\n\r";
		String strImage = "edit ";
		boolean flag = true;
		int index = 0;
		while (flag) {
			if (string.startsWith(strBlank)) {
				if (string.equals(strBlank)) {
					flag = false;
				} else {
					string = string.substring(strBlank.length(),
							string.length());
				}

			} else if (string.startsWith(strNextLine)) {
				if (string.equals(strNextLine)) {
					flag = false;
				} else {
					string = string.substring(strNextLine.length(),
							string.length());
				}
			} else if (string.startsWith(strImage)) {
				if (string.equals(strImage)) {
					flag = false;
				} else {
					string = string.substring(strImage.length(),
							string.length());
				}
			}
			index++;
		}
		return index;
	}
    
	/*
	 * @param string 原字符串
	 * @param insertString  插入的字符串
	 * @param insertIndex  插入的位置(计算获得)
	 * @return  组合后的字符串
	 */
	public static String insertString(String string, String insertString,
			int insertIndex) {
		String strBlank = "insert_an_blank.png";
		string = string.substring(0, strBlank.length() * insertIndex)
				+ insertString
				+ string.substring(strBlank.length() * insertIndex,
						string.length());
		return string;
	}
	
	/*
	 * @param string 原字符串
	 * @param insertString 要插入的字符串
	 * @param insertIndex 插入的位置
	 * @return
	 */
	public static String insertString2(String string, String insertString,
			int insertIndex) {
		String strBlank = " ";
		String strNextLine = "\n\r";
		String strImage = "edit ";
		StringBuffer buffer = new StringBuffer();
		int count = 0;
		boolean flag = true;
		while (flag) {
			if (count == insertIndex) {
				buffer.append(insertString);
			}
			if (string.startsWith(strBlank)) {
				if (string.equals(strBlank)) {
					flag = false;
				} else {
					string = string.substring(strBlank.length(),
							string.length());
				}
				count++;
				buffer.append(strBlank);
			} else if (string.startsWith(strNextLine)) {
				if (string.equals(strNextLine)) {
					flag = false;
				} else {
					string = string.substring(strNextLine.length(),
							string.length());
				}
				count++;
				buffer.append(strNextLine);
			} else if (string.startsWith(strImage)) {
				if (string.equals(strImage)) {
					flag = false;
				} else {
					string = string.substring(strImage.length(),
							string.length());
				}
				count++;
				buffer.append(strImage);
			}
		}
		return buffer.toString();
	}

}
